package com.sike.javacore.serializer.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;

public class ProtoBufTest01 {
    public static void main(String[] args) throws InvalidProtocolBufferException {
        // 首先需要获取构造器
        StudentEntity.Student.Builder builder = StudentEntity.Student.newBuilder();
        // 设置属性值
        builder.setId(1);
        builder.setName("张三");
        builder.setSex("1");
        builder.addHobbybes("足球");
        builder.addHobbybes("篮球");

        // 创建对象
        StudentEntity.Student  student = builder.build();
        // 序列化
        byte[] data = student.toByteArray();
        System.out.println("序列化内容");
        for(byte b : data){
            System.out.print(b);
        }

        System.out.println();
        System.out.println("==============================");
        System.out.println("反序列化内容");

        // 反序列化
        StudentEntity.Student student1 = StudentEntity.Student.parseFrom(data);
        System.out.println("id：" + student1.getId());
        System.out.println("sex：" + student1.getSex());
        System.out.println("name：" + student1.getName());
        System.out.println("hobbybes：" + student1.getHobbybesList().toString());
    }
}
